<!--智慧消防V1.0-edit页面-->
<template>
    <div class="app-container">
        <div class="body-title">设备维保周期配置{{createStatus?'新增':'编辑'}}</div>
        <el-row :gutter="10">
            <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                <el-form :model="cycleForm" ref="cycleForm" :rules="cycleFormRules" label-width="100px">
                    <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                        <el-form-item label="名称" prop="name">
                            <el-input type="text" v-model="cycleForm.name" placeholder="请输入名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                        <el-form-item label="维保周期" prop="cycle">
                            <el-select style="width: 100%" v-model="cycleForm.cycle" ref="selectUpResId" placeholder="请选择父级资源">
                              <el-option
                                  v-for="dict in scycleOptions"
                                  :key="dict.dictValue"
                                  :label="dict.dictLabel"
                                  :value="dict.dictValue"
                              />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                        <el-form-item label="完成率" prop="percent">
                            <el-input type="text" v-model="cycleForm.percent" placeholder="请输入完成率" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                        <el-form-item label="描述" prop="described">
                            <el-input type="text" v-model="cycleForm.described" placeholder="请输入资源标题 如 /登录中心" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-col>
            <div style="text-align: center" class="dialog-footer">
                <el-button @click="cancelCycle">取 消</el-button>
                <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createCycle">创 建</el-button>
                <el-button :loading="addLoading" v-else type="primary" @click="updateCycle">修 改</el-button>
            </div>
        </el-row>
    </div>
</template>

<script>
import {addInCycle, getInCycle, updateInCycle} from "@/api/cycle";
import {getInUserVo} from "@/api/enterprise";

export default {
    name: "CycleEdit",
    data(){
        return{
            cycleForm:{
              id: "",
              ucId: "",
              name: "",
              cycle: "",
              percent: 0,
              described: "",

            },
            cycleFormRules:{

            },
            //父级资源数据
            scycleOptions:[],
            //资源类型
            CycleType:[
                {
                    value:'menu',
                    label:'菜单'
                },
                {
                    value:'button',
                    label:'按钮'
                }
            ],
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            addLoading:false
        }
    },
    computed:{
        createStatus(){
            return this.$route.params.ucId =='0'
        }
    },
    created() {
      this.getDicts("cycle_type").then(response => {
        this.scycleOptions = response.data.data;
      });

      if (!this.createStatus) {
        let ucId = this.$route.params.ucId
        this.getCycle(ucId)
      }
    },
    methods:{
        cancelCycle(){
          window.history.back()
        },
        createCycle(){
            this.addLoading = true
            this.$refs['cycleForm'].validate(valid => {
                if (valid){
                    addInCycle(this.cycleForm).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加设备维保周期成功"
                        })
                        this.cancelCycle()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updateCycle(){
          this.addLoading = true
          this.$refs['cycleForm'].validate(valid => {
            if (valid){

              updateInCycle(this.cycleForm).then(res=>{
                this.addLoading = false
                this.$notify.success({
                  title: '成功',
                  message: "修改设备维保周期成功"
                })
                this.cancelCycle()
              }).catch(res=>{
                this.addLoading = false
                this.$notify.error({
                  title: '失败',
                  message: res.msg
                })
              })
            }else {
              this.addLoading = false
              console.log("表单验证不通过")
            }
          })
        },
        getCycle(value){
          getInCycle(value).then(res => {
            this.cycleForm = res.data.data
          }).catch(res => {

          })
        }
    }
}
</script>

<style scoped>

</style>
